package com.felix.thread.philosopher;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * DeadLockingDiningPhilosopher
 *
 * @author xuejinhua
 * @date 2018/10/8 17:32
 */
public class DeadLockingDiningPhilosopher {

    public static void main(String[] args) {
        int size = 3;
        ExecutorService executorService = Executors.newCachedThreadPool();
        Chopstick[] sticks = new Chopstick[5];
        for (int i = 0; i < size; i++) {
            sticks[i] = new Chopstick("筷子" + i);
        }
        for (int i = 0; i < size; i++) {
            if (i < size - 1) {
                executorService.execute(new Philosopher(sticks[i], sticks[(i + 1) % size], "哲学家" + i));
            } else {
                executorService.execute(new Philosopher(sticks[0], sticks[1], "哲学家" + i));
            }
        }
        executorService.shutdown();
    }
}
